#include "GreyscaleGridSampler.h"
#include <zxing/ReaderException.h>
#include <sstream>

namespace newcode {

GreyscaleGridSampler GreyscaleGridSampler::gridSampler;

GreyscaleGridSampler::GreyscaleGridSampler()
{
}

Ref<ByteMatrix> GreyscaleGridSampler::sampleGrid(Ref<ByteMatrix> image,
    int dimension, Ref<PerspectiveTransform> transform)
{
    Ref<ByteMatrix> bytes(new ByteMatrix(dimension, dimension));
    vector<float> points(dimension << 1, (const float)0.0f);
    for (int y = 0; y < dimension; y++)
    {
        int max = points.size();
        float yValue = (float)y + 0.5f;
        for (int x = 0; x < max; x += 2)
        {
            points[x] = (float)(x >> 1) + 0.5f;
            points[x + 1] = yValue;
        }
        transform->transformPoints(points);
        checkAndNudgePoints(image, points);
        for (int x = 0; x < max; x += 2)
        {
            (*bytes)(x >> 1, y) = (*image)((int)points[x], (int)points[x + 1]);
        }
    }
    return bytes;
}

Ref<ByteMatrix> GreyscaleGridSampler::sampleGrid(Ref<ByteMatrix> image,
    int dimension, float p1ToX, float p1ToY, float p2ToX, float p2ToY,
    float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX,
    float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY,
    float p4FromX, float p4FromY)
{
    Ref<PerspectiveTransform> transform(
        PerspectiveTransform::quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX,
            p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX,
            p2FromY, p3FromX, p3FromY, p4FromX, p4FromY));

    return sampleGrid(image, dimension, transform);
}

void GreyscaleGridSampler::checkAndNudgePoints(Ref<ByteMatrix> image,
vector<float> &points)
{
    int width = image->getY();
    int height = image->getX();

    for (size_t offset = 0; offset < points.size(); offset += 2)
    {
        int x = (int)points[offset];
        int y = (int)points[offset + 1];
        if (x < -1 || x > width || y < -1 || y > height)
        {
            ostringstream s;
            s << "Transformed point out of bounds at " << x << "," << y;
            throw ReaderException(s.str().c_str());
        }

        if (x == -1)
        {
            points[offset] = 0.0f;
        }
        else if (x == width)
        {
            points[offset] = width - 1;
        }
        if (y == -1)
        {
            points[offset + 1] = 0.0f;
        }
        else if (y == height)
        {
            points[offset + 1] = height - 1;
        }
    }
}

GreyscaleGridSampler &GreyscaleGridSampler::getInstance()
{
    return gridSampler;
}

}
